﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LeadTeam.Exam.AdminTool.Forms;
using LeadTeam.Exam.Entities;

namespace LeadTeam.Exam.AdminTool
{
    public partial class LookupOrganizations : Form
    {
        private LookupOrganizationsController _controllor;

        public LookupOrganizationsController Controllor
        {
            get { return _controllor; }
            set
            {
                this._controllor = value;
                LoadModel();
            }
        }
        ILookupOrganizations iLookupView;
        public LookupOrganizations(ILookupOrganizations iLookupView)
        {
            InitializeComponent();
            this.iLookupView = iLookupView;
        }

        public void LoadModel()
        {
            IList<Organizations> list = _controllor.model;
            IList<Organizations> listRoot = list.Where(p => (p.O_PARENT_ID == 0)).ToList<Organizations>();
            foreach (Organizations org in listRoot)
            {
                TreeNode newNode = new TreeNode(org.O_NAME);
                newNode.Tag = org.O_ID;
                newNode.ImageIndex = 0;
                tvOrg.Nodes.Add(newNode);
                LookChildeNode(newNode, org.O_ID, list);
            }
            tvOrg.ExpandAll();
        }

        private void LookChildeNode(TreeNode parentNode, int id, IList<Organizations> list)
        {
            foreach (Organizations org in list)
            {
                if (org.O_PARENT_ID == id)
                {
                    TreeNode newNode = new TreeNode(org.O_NAME);
                    newNode.Tag = org.O_ID;
                    newNode.ImageIndex = 0;
                    parentNode.Nodes.Add(newNode);

                    LookChildeNode(newNode, org.O_ID, list);
                }
            }
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            if (tvOrg.SelectedNode != null)
            {
                iLookupView.SetOrganizations(Convert.ToInt32(tvOrg.SelectedNode.Tag), tvOrg.SelectedNode.Text);
            }
            this.Close();
        }
    }
}
